package com.zzb.springcloud.controller;

import com.zzb.springcloud.common.CommonResult;
import com.zzb.springcloud.entity.Payment;
import com.zzb.springcloud.interfaces.LoadBanlance;
import com.zzb.springcloud.service.payment.PaymentServiceFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 订单业务数据交互
 * @CreateTime 2021/4/16 0016 16:31
 * @Author 不浪小生
 */
@Slf4j
@RestController
public class OrderCtr {

    private static final String PAYMENT_HOST = "http://CLOUD-PROVIDER-PAYMENT";

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Resource
    private LoadBanlance loadBanlance;
    @Resource
    private PaymentServiceFeign paymentServiceFeign;

    @GetMapping("/consumer/payment/reqserver")
    public CommonResult loadBanlance() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-PAYMENT");
        ServiceInstance instance = loadBanlance.getInstance(instances);
        return restTemplate.getForObject(instance.getUri() + "/payment/loadbanlance", CommonResult.class);
    }

    @PostMapping("/consumer/payment/save")
    public CommonResult save(@RequestBody Payment payment) {
        return restTemplate.postForObject(PAYMENT_HOST + "/payment/save", payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/getPayment/{id}")
    public CommonResult getPayment(@PathVariable Long id) {
        return restTemplate.getForObject(PAYMENT_HOST + "/payment/getPayment/" + id,CommonResult.class, id);
    }

    @GetMapping("/consumer/payment/getPaymentForEntity/{id}")
    public CommonResult getPaymentForEntity(@PathVariable Long id) {
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_HOST + "/payment/getPayment/" + id,CommonResult.class, id);
        if (entity.getStatusCode().is2xxSuccessful()) {
            return entity.getBody();
        } else {
            return new CommonResult(444, "操作失败");
        }
    }

    @GetMapping("/payment/getDiscovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("Eureka注册中心服务:" + service);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-CONSUMER-ORDER80");
        for (ServiceInstance instance : instances) {
            log.info("实例参数信息:" + instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }

        return discoveryClient;
    }

    @GetMapping("/order80/payment/getPaymentFeign/{id}")
    public CommonResult getPaymentFeign(@PathVariable("id") Long id) {
        return paymentServiceFeign.getPayment(id);
    }

    @GetMapping("/order80/payment/paymentTimeout")
    public String paymentTimeout() {
        return paymentServiceFeign.paymentTimeout();
    }

}
